$(document).ready(function() {
    $(document).on('click', '#downloadGridByStation', function () {
        exportGridToCsv(this, function() {
            return 'Usuarios';
        });
    });

    dataSource = new kendo.data.DataSource({
        serverPaging: true,
        serverSorting: true,
        transport: {
            read: {
                url: appBosch.crud.read,
                dataType: "json"
            },
            destroy: {
                url: appBosch.crud.update,
                dataType: "json"
            },
            parameterMap: function(options, operation) {
        
                var params = {};
                switch (operation)
                {
                    case 'create':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'read':
                        break;
                    case 'update':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'destroy':
                        params = {id: options.models[0].id};
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "id",
                fields: {
                    nomina: {
                        editable: true,
                        nullable: true,
                        validation: {
                            required: true
                        }
                    },
                    curp: {
                        editable: true,
                        nullable: true,
                        validation: {
                            required: true
                        }
                    },
                    full_name: {
                        editable: false,
                        nullable: false,
                        validation: {
                            required: false
                        }
                    },
                    name: {
                        editable: true,
                        nullable: true,
                        validation: {
                            required: true
                        }
                    },
                    last_name_1: {
                        editable: true,
                        nullable: true,
                        validation: {
                            required: true
                        }
                    },
                    last_name_2: {
                        editable: true,
                        nullable: true,
                        validation: {
                            required: true
                        }
                    },
                    admission_date: {
                        editable: true,
                        nullable: true,
                        type: "date",
                        validation: {
                            required: true
                        }
                    },
                    support: {
                        editable: true,
                        nullable: false,
                        type: "boolean",
                        defaultValue: false
                    },
                    is_external: {
                        editable: true,
                        nullable: false,
                        type: "boolean",
                        defaultValue: false
                    },
//                    preparador_id: {
//                        editable: true,
//                        nullable: false,
//                        defaultValue: appBosch.preparadores[0].value
//                    },
                    category_id: {
                        editable: true,
                        nullable: false,
                        defaultValue: appBosch.categories[0].value
                    },
                    moe_id: {
                        editable: true,
                        nullable: false,
                        defaultValue: appBosch.moes[0].value
                    },
                    area_id: {
                        editable: true,
                        nullable: false,
                        defaultValue: appBosch.areas[0].value
                    },
                    line_id: {
                        editable: true,
                        nullable: false,
                        defaultValue: appBosch.lines[0].value
                    },
                    shift_id: {
                        editable: true,
                        nullable: false,
                        defaultValue: appBosch.shifts[0].value
                    }
                }
            }
        }
    });

    $("#grid").kendoGrid({
        height: 540,
        reorderable: true,
        resizable: true,
        filterable: true,
        columnMenu: true,
        sortable: true,
//        pageable: false,
        selectable: true,
        dataSource: dataSource,
        toolbar: null,
        dataBound: dataBound,
        scrollable: {
            virtual: true
        },
        columns: [
            {
                field: "nomina",
                title: "Nomina",
                width: 100,
                locked: true
            },
            {
                field: "curp",
                title: "C.U.R.P",
                width: 180,
                locked: true
            },
            {
                field: "full_name",
                title: "Nombre completo",
                width: 350
            },
            {
                field: "name",
                title: "Nombre(s)",
                width: 150
            },
            {
                field: "last_name_1",
                title: "Apellido 1",
                width: 150
            },
            {
                field: "last_name_2",
                title: "Apellido 2",
                width: 150
            },
//            {
//                field: "preparador_id",
//                values: appBosch.preparadores,
//                title: "Preparador",
//                width: 350
//            },
            {
                field: "category_id",
                title: "Rol",
                values: appBosch.categories,
                width: 150
            },
            {
                field: "moe_id",
                title: "MOE",
                values: appBosch.moes,
                width: 150
            },
            {
                field: "area_id",
                title: "Área",
                values: appBosch.areas,
                width: 150
            },
            {
                field: "line_id",
                title: "Línea",
                values: appBosch.lines,
                width: 150
            },
            {
                field: "admission_date",
                title: "Fecha de ingreso",
                width: 150,
                template: "#=kendo.toString(admission_date, 'd') #"
            },
            {
                field: "shift_id", title: "Turno",
                values: appBosch.shifts,
                width: 150
            },
            {
                field: "support",
                title: "Soporte",
                width: 150,
                template: "#if(support){ #<i class='fa fa-check'></i> #}#"
            },
            {
                field: "is_external",
                title: "Externo",
                width: 150,
                template: "#if(is_external){ #<i class='fa fa-check'></i> #}#"
            },
            {
                command: [
                  
//                    {
//                        edit: "update",
//                        text: "Reactivar",
//                        template: "<a title='Editar' class='k-button k-button-icontext k-grid-edit' href=''><i class='fa fa-pencil'></i></a>",
//                        text: {
//                            edit: "",
//                            update: "",
//                            cancel: ""
//                        }
//                    }
                    {
                        name:"destroy",
//                        text: "Restaurar"
                        template: "<a title='Restaurar' class='k-button k-button-icontext k-grid-delete' href=''>Restaurar</a>"
                    }
                ], title: "&nbsp;", width: 230, locked: true
            }
        ],
        editable: {
            confirmation: "¿Estas seguro que deseas restaurar este usuario?",
            mode: "popup"
        }
    });
});

function dataBound() {
    var filter = this.dataSource.filter();
    this.thead.find(".k-header-column-menu.k-state-selected").removeClass("k-state-selected");
    if (filter) {
        var filteredMembers = {};
        setFilteredMembers(filter, filteredMembers);
        this.thead.find("th[data-field]").each(function() {
            var cell = $(this);
            var filtered = filteredMembers[cell.data("field")];
            if (filtered) {
                cell.find(".k-header-column-menu").addClass("k-state-selected");
            }
        });
    }
}


function setFilteredMembers(filter, members) {
    if (filter.filters) {
        for (var i = 0; i < filter.filters.length; i++) {
            setFilteredMembers(filter.filters[i], members);
        }
    }
    else {
        members[filter.field] = true;
    }
}
